using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using KFGame;
using FairyGUI;

namespace KFGame
{
    /// <summary>
    /// 设置页，配置参数据，提供游戏应用，通过配置文件管理
    /// 
    /// </summary>
    public class KFSettingView : KFBackViewManager
    {

        struct SettingItem
        {
            public KFSettingItem item;
            public GComponent view;
        }

        // fairyGUI组件
        GComponent _mainView;
        // fairyGUI列表组件
        GList _list;

        //选择的行
        int selectRow = 0;

        //总项数量
        int itemsCount = 0;

        //按钮数量
        int btnnum = 3;



        //关联列表
        List<SettingItem> items = new List<SettingItem>();


        public FairyGUI.UIPanel panel;


         
        /// <summary>
        /// 起动时行初始化
        /// </summary>
        public void Start()
        {
            initView();

            panel.packageName = "BackUI";
            panel.componentName = "SettingView";
            panel.CreateUI();
            _mainView = panel.ui;

            _list = _mainView.GetChild("list").asList;

            GButton closebtn = _mainView.GetChild("close").asButton;
            closebtn.onClick.Add(toClose);


            GButton savebtn = _mainView.GetChild("save").asButton;
            savebtn.onClick.Add(saveDatas);

            GButton restbtn = _mainView.GetChild("rest").asButton;
            restbtn.onClick.Add(restData);
            // _list.itemRenderer = RenderListItem;
            // _list.SetVirtual();

            Init();


        }

        /// <summary>
        /// 由框加推送的事件
        /// </summary>
        /// <param name="e"></param>
        public override void OnEvent(KFEvent e)
        {
            Debug.Log("setting :" + e.name);

            //向上
            if (e.name == KFEEVENTS.KFE_EVENT_TO_UP)
            {
                selectRow--;
                if (selectRow < 0)
                    selectRow = itemsCount - 1;

                selectItem(selectRow);
                return;
            }
            //向下
            if (e.name == KFEEVENTS.KFE_EVENT_TO_DOWN ||
                e.name == KFEEVENTS.KFE_EVENT_TO_OK)
            {

                selectRow++;
                if (selectRow >= itemsCount)
                    selectRow = 0;

                selectItem(selectRow);
                return;
            }
            //减小
            if (e.name == KFEEVENTS.KFE_EVENT_TO_LEFT)
            {
                acReduce();
                return;
            }
            //增加
            if (e.name == KFEEVENTS.KFE_EVENT_TO_RIGHT)
            {
                acAdd();
                return;
            }

            base.OnEvent(e);
        }

        void acReduce()
        {
            int sbtnnum = items.Count - selectRow;
            if (sbtnnum <= 0)
            {
                if (sbtnnum == 0)
                {
                    //cose 
                    toClose();
                }
                if (sbtnnum == -1)
                {
                    //rest 
                    restData();
                }
                if (sbtnnum == -2)
                {
                    //save 
                    saveDatas();
                }
                return;
            }

            if (items.Count == 0)
                return;

            if (items[selectRow].item.value_type == "bool")
            {
                ItemClickValue(items[selectRow].item, items[selectRow].view);
                ItemClickSetView(items[selectRow].item, items[selectRow].view);
                return;
            }
            if (items[selectRow].item.value_type == "integer")
            {

                ItemCutValue(items[selectRow].item, items[selectRow].view);
                return;
            }
        }

        void acAdd()
        {
            int sbtnnum = items.Count - selectRow;
            if (sbtnnum <= 0)
            {
                if (sbtnnum == 0)
                {
                    //cose 
                    toClose();
                }
                if (sbtnnum == -1)
                {
                    //rest
                    restData();
                }
                if (sbtnnum == -2)
                {
                    //save 
                    saveDatas();
                }
                return;
            }

            if (items.Count == 0)
                return;

            if (items[selectRow].item.value_type == "bool")
            {
                ItemClickValue(items[selectRow].item, items[selectRow].view);
                ItemClickSetView(items[selectRow].item, items[selectRow].view);
                return;
            }
            if (items[selectRow].item.value_type == "integer")
            {
                ItemAddValue(items[selectRow].item, items[selectRow].view);
                return;
            }
        }

        bool selectbtn(int i)
        {
            GButton closebtn = _mainView.GetChild("close").asButton;
            GGraph cblock = closebtn.GetChild("select_block").asGraph;
            cblock.visible = false;

            GButton savebtn = _mainView.GetChild("save").asButton;
            GGraph sblock = savebtn.GetChild("select_block").asGraph;
            sblock.visible = false;

            GButton restbtn = _mainView.GetChild("rest").asButton;
            GGraph restblock = restbtn.GetChild("select_block").asGraph;
            restblock.visible = false;

            int sbtnnum = items.Count - i;
            if (sbtnnum <= 0)
            {
                if (sbtnnum == 0)
                {
                    //cose 
                    cblock.visible = true;

                }
                if (sbtnnum == -1)
                {
                    //rest
                    restblock.visible = true;
                }
                if (sbtnnum == -2)
                {
                    //save 
                    sblock.visible = true;
                }
                return true;
            }
            return false;
        }

        void selectItem(int i)
        {

            foreach (var item in items)
            {
                GGraph b = item.view.GetChild("select_block").asGraph;
                b.visible = false;
            }

            if (selectbtn(i))
                return;

            if (items.Count == 0)
                return;

            GGraph block = items[i].view.GetChild("select_block").asGraph;
            block.visible = true;
        }

        public void toClose()
        {

            close();
        }

        /// <summary>
        /// 重置参数据
        /// </summary>
        void restData()
        {
            Debug.Log("rest datas ");
            foreach (var item in items)
            {
                item.item.value = item.item.value_default;
                if (item.item.value_type == "bool")
                {
                    ItemClickSetView(item.item, item.view);
                }
                else
                {
                    setValue(item.item, item.view);
                }
            }
             
        }
        /// <summary>
        /// 设置数据进行保存
        /// </summary>
        void saveDatas()
        { 
            KF.setting.save();
        }



        /// <summary>
        /// 初始化数据
        /// </summary>
        void Init()
        {
            List<string> names = KF.setting.getItems();
            itemsCount = names.Count + btnnum;

            foreach (var n in names)
            {
                KFSettingItem item = KF.setting.getItem(n);
                if (item != null)
                {
                    if (item.value_type == "bool")
                    {
                        BoolItem(item);
                        continue;
                    }
                    if (item.value_type == "integer")
                    {
                        IntegreItem(item);
                        continue;
                    }
                }
            }

            selectItem(selectRow);
        }


        /// <summary>
        /// 开关型  选项卡
        /// </summary>
        /// <param name="item"></param>
        void BoolItem(KFSettingItem item)
        {
            GComponent gobj = UIPackage.CreateObject("BackUI", "settingonoff").asCom;
            GTextField gt = gobj.GetChild("title").asTextField;
            gt.text = item.title;


            GObject info = gobj.GetChild("info");
            if (info != null)
                info.asTextField.text = item.describe;


            GButton c = gobj.GetChild("value").asButton;
            if (item.value == "1")
                c.selected = true;

            GGraph block = gobj.GetChild("select_block").asGraph;
            block.visible = false;

            SettingItem itemv = new SettingItem();
            itemv.item = item;
            itemv.view = gobj;
            items.Add(itemv);

            c.onClick.Add(() => { ItemClickValue(item, gobj); });
            _list.AddChild(gobj);
        }

        /// <summary>
        /// 开关形按钮被点击
        /// </summary>
        /// <param name="item"></param>
        /// <param name="gobj"></param>
        void ItemClickValue(KFSettingItem item, GComponent gobj)
        {
            if (item.value == "1")
                item.value = "0";
            else
                item.value = "1";
        }

        /// <summary>
        /// 开关型 设置显视状态
        /// </summary>
        /// <param name="item"></param>
        /// <param name="gobj"></param>
        void ItemClickSetView(KFSettingItem item, GComponent gobj)
        {
            GButton c = gobj.GetChild("value").asButton;
            if (item.value == "1")
                c.selected = true;
            else
                c.selected = false;
        }

        /// <summary>
        /// 数值型 输入型框
        /// </summary>
        /// <param name="item"></param>
        void IntegreItem(KFSettingItem item)
        {
            GComponent gobj = UIPackage.CreateObject("BackUI", "SettingInput").asCom;
            GTextField gt = gobj.GetChild("title").asTextField;
            gt.text = item.title;
            GTextField info = gobj.GetChild("info").asTextField;
            info.text = item.describe;

            GTextInput valueV = gobj.GetChild("value").asTextInput;
            valueV.text = item.value.ToString();

            GButton addbtn = gobj.GetChild("add").asButton;
            addbtn.onClick.Add(() => { ItemAddValue(item, gobj); });

            GButton cutbtn = gobj.GetChild("cut").asButton;
            cutbtn.onClick.Add(() => { ItemCutValue(item, gobj); });

            GGraph block = gobj.GetChild("select_block").asGraph;
            block.visible = false;

            SettingItem itemv = new SettingItem();
            itemv.item = item;
            itemv.view = gobj;
            items.Add(itemv);

            _list.AddChild(gobj);
        }


        public void setValue(KFSettingItem item, GComponent gobj)
        {
            gobj.GetChild("value").asTextInput.text = item.value;
        }

        /// <summary>
        /// 点加号
        /// </summary>
        /// <param name="item"></param>
        /// <param name="gobj"></param>
        public void ItemAddValue(KFSettingItem item, GComponent gobj)
        {

            double v = Convert.ToDouble(item.value);
            v += item.value_step;
            if (v > item.value_max)
                v = item.value_max;

            item.value = v.ToString();

            gobj.GetChild("value").asTextInput.text = item.value;

        }

        /// <summary>
        /// 点减号
        /// </summary>
        /// <param name="item"></param>
        /// <param name="gobj"></param>
        public void ItemCutValue(KFSettingItem item, GComponent gobj)
        {

            double v = Convert.ToDouble(item.value);
            v -= item.value_step;
            if (v < item.value_mix)
                v = item.value_mix;

            item.value = v.ToString();

            gobj.GetChild("value").asTextInput.text = item.value;

        }
    }

}